$().ready(function () {
    getPhoto();
});
var FIXED_LENGTH = 16;
function getPhoto() {
    var lastOID = null;
    $.ajax({
        url:"/api/v1/show_photo",
        type:"GET",
        data:{
            lastPhotoId:lastOID
        },
        success : function (result) {
            //相册不为空
            if(result != null){
                handleImg(result);
            }
        }
    })
}

function handleImg(result) {
    //处理默认类型
    if(result['default']){
        // console.log(result['default']);
        $('#stack_default').empty();
        handleImgByType(result['default']);

    }else {

    }
    //处理不舍类型
    if(result['unwilling']){
        // console.log(result['unwilling']);
        $('#stack_unwilling').empty();
        handleImgByType(result['unwilling']);

    }else {

    }
    //处理喜悦类型
    if(result['joyful']){
        // console.log(result['joyful']);
        $('#stack_joyful').empty();
        handleImgByType(result['joyful']);
    }else {

    }
    //处理日常类型
    if(result['everyday']){
        // console.log(result['everyday']);
        $('#stack_everyday').empty();
        handleImgByType(result['everyday']);
    }else {

    }
    //处理 纪念 类型
    if(result['commemorate']){
        // console.log(result['commemorate']);
        $('#stack_commemorate').empty();
        handleImgByType(result['commemorate']);
    }else {

    }
}

function handleImgByType(data) {
    var STACK_DEFAULT  = $('#stack_default');
    var STACK_UNWILLING = $('#stack_unwilling');
    var STACK_JOYFUL = $('#stack_joyful');
    var STACK_EVERYDAY = $('#stack_everyday');
    var STACK_COMMEMORATE = $('#stack_commemorate');
    data.forEach(function (value) {
        if(value['type'] === "default"){
            var description = showDescription(value['description']);
            STACK_DEFAULT.append("<div class='item'>" +
                "<div class='item__content'>" +
                "<img src='"+ value['photoPath'] +"'/>" +
                "<h3 class='item__title'>"+ value['title'] +"<span class='item__date'>"+ value['timestamp'] +"</span></h3>" +
                "<div class='item__details'>" +
                "<ul>" +
                "<li><i class='fa fa-heart'><p>"+ description +"</p></i></li>" +
                "</ul>" +
                "</div>" +
                "</div>" +
                "</div>");
        }
        if(value['type'] === "unwilling"){
            var description = showDescription(value['description']);
            STACK_UNWILLING.append("<div class='item'>" +
                "<div class='item__content'>" +
                "<img src='"+ value['photoPath'] +"'/>" +
                "<h3 class='item__title'>"+ value['title'] +"<span class='item__date'>"+ value['timestamp'] +"</span></h3>" +
                "<div class='item__details'>" +
                "<ul>" +
                "<li><i class='fa fa-heart'><p>"+ description +"</p></i></li>" +
                "</ul>" +
                "</div>" +
                "</div>" +
                "</div>");
        }
        if(value['type'] === "joyful"){
            var description = showDescription(value['description']);
            STACK_JOYFUL.append("<div class='item'>" +
                "<div class='item__content'>" +
                "<img src='"+ value['photoPath'] +"'/>" +
                "<h3 class='item__title'>"+ value['title'] +"<span class='item__date'>"+ value['timestamp'] +"</span></h3>" +
                "<div class='item__details'>" +
                "<ul>" +
                "<li><i class='fa fa-heart'><p>"+ description +"</p></i></li>" +
                "</ul>" +
                "</div>" +
                "</div>" +
                "</div>");
        }
        if(value['type'] === "everyday"){
            var description = showDescription(value['description']);
            STACK_EVERYDAY.append("<div class='item'>" +
                "<div class='item__content'>" +
                "<img src='"+ value['photoPath'] +"'/>" +
                "<h3 class='item__title'>"+ value['title'] +"<span class='item__date'>"+ value['timestamp'] +"</span></h3>" +
                "<div class='item__details'>" +
                "<ul>" +
                "<li><i class='fa fa-heart'><p>"+ description +"</p></i></li>" +
                "</ul>" +
                "</div>" +
                "</div>" +
                "</div>");
        }
        if(value['type'] === "commemorate"){
            var description = showDescription(value['description']);
            STACK_COMMEMORATE.append("<div class='item'>" +
                "<div class='item__content'>" +
                "<img src='"+ value['photoPath'] +"'/>" +
                "<h3 class='item__title'>"+ value['title'] +"<span class='item__date'>"+ value['timestamp'] +"</span></h3>" +
                "<div class='item__details'>" +
                "<ul>" +
                "<li><i class='fa fa-heart'><p>"+ description +"</p></i></li>" +
                "</ul>" +
                "</div>" +
                "</div>" +
                "</div>");
        }
    })
}

/**
 *展示描述信息
 */
function showDescription(str) {
    console.log("str :" + str);
    console.log("length : " +str.length);
    if(str.length >= FIXED_LENGTH){
        return handleLongText(str);
    }else {
        return handleShortText(str);
    }
}
/**
 * 处理短文本
 * @param str
 */
function handleShortText(str) {
    return '<br><span>'+str+'</span>';
}
/**
 * 处理长文本
 * @param str
 */
function handleLongText(str) {
    var desc ;
    var span;
    var strLength = str.length;
    while (str.length > FIXED_LENGTH){
        desc = str.slice(0,FIXED_LENGTH);
        str = str.slice(FIXED_LENGTH,strLength);
        span += '<br><span>'+ desc +'</span>';
    }
    span += '<br><span>'+ str +'</span>';
    return span;
}